﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    /// <summary>
    /// 图片轮播控件
    /// </summary>
    public class PageCommon_Swiper:ControlBase
    {
        private List<SwiperItem> _imageList=new List<SwiperItem>();
        private string _paginationStyle = ".swiper-pagination";
        private int _playInterval = 10000;

        public PageCommon_Swiper(ViewContext viewContext) : base(viewContext)
        {
            this.Css("swiper-container weui-cell_swiper");

            viewContext.ViewBag.Swiper = true;
        }

        public PageCommon_Swiper Images(IEnumerable<string> urls)
        {
            _imageList.AddRange(urls.Select(x=>new SwiperItem(x)));

            return this;
        }

        public PageCommon_Swiper Images(IEnumerable<SwiperItem> urls)
        {
            _imageList.AddRange(urls);

            return this;
        }

        public PageCommon_Swiper Images<TData>(IEnumerable<TData> lst,Func<TData,string> urlGetter, Func<TData, string> hrefGetter)
        {
            _imageList.AddRange(lst.Select(x=>new SwiperItem(urlGetter(x), hrefGetter(x))));

            return this;
        }

        public PageCommon_Swiper PaginationStyle(string className)
        {
            _paginationStyle = className;

            return this;
        }

        /// <summary>
        /// 图片切换时间间隔,默认为1000ms
        /// </summary>
        /// <param name="playInterval"></param>
        /// <returns></returns>
        public PageCommon_Swiper Autoplay(int playInterval)
        {
            _playInterval = playInterval;

            return this;
        }

        public override MvcHtmlString Render()
        {
            /*
             <div class="swiper-container" data-space-between='10' data-pagination='.swiper-pagination' data-autoplay="1000">
               <div class="swiper-wrapper">
               <div class="swiper-slide"><img src="//gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i1/TB1n3rZHFXXXXX9XFXXXXXXXXXX_!!0-item_pic.jpg_320x320q60.jpg" alt=""></div>
               <div class="swiper-slide"><img src="//gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i4/TB10rkPGVXXXXXGapXXXXXXXXXX_!!0-item_pic.jpg_320x320q60.jpg" alt=""></div>
               <div class="swiper-slide"><img src="//gqianniu.alicdn.com/bao/uploaded/i4//tfscom/i1/TB1kQI3HpXXXXbSXFXXXXXXXXXX_!!0-item_pic.jpg_320x320q60.jpg" alt=""></div>
               </div>
            </div>
             */

            ViewContext.ViewBag.HasUseSwiper = true;

            ViewContext.Writer.WriteLine($"<div {_attrs} data-space-between=\'10\' data-autoplay=\"{_playInterval}\">");
            ViewContext.Writer.WriteLine("<div class=\"swiper-wrapper\">");

            foreach (var img in _imageList)
            {
                ViewContext.Writer.WriteLine($"<div class=\"swiper-slide\"><a href=\"{img.GotoUrl}\"><img width=\"100%\" src=\"{img.ImageUrl}\" alt=\"\"></a></div>");
            }

            ViewContext.Writer.WriteLine("</div> <!-- end swiper-wrapper -->");

            ViewContext.Writer.WriteLine("<div class=\"swiper-pagination\"></div>");

            ViewContext.Writer.WriteLine("</div> <!-- end swiper-container -->");

            return MvcHtmlString.Empty;
        }
    }

    /// <summary>
    /// 轮播控件,使用容器的方式输出轮播子项
    /// </summary>
    public class PageCommon_SwiperContainer : ContainerBase
    {
        private string _paginationStyle = "swiper-pagination";
        private int _playInterval = 10000;

        public PageCommon_SwiperContainer(ViewContext viewContext) : base(viewContext)
        {
            this.Css("swiper-container weui-cell_swiper");

            viewContext.ViewBag.Swiper = true;
        }

        public PageCommon_SwiperContainer PaginationStyle(string className)
        {
            _paginationStyle = className;

            return this;
        }

        /// <summary>
        /// 图片切换时间间隔,默认为1000ms
        /// </summary>
        /// <param name="playInterval"></param>
        /// <returns></returns>
        public PageCommon_SwiperContainer Autoplay(int playInterval)
        {
            _playInterval = playInterval;

            return this;
        }

        /// <summary>
        /// 使用容器的方式输出swiper子项
        /// </summary>
        /// <returns></returns>
        public ContainerBase RenderItem()
        {
            var container=new PageCommon_SwiperContainerItem(ViewContext);

            return container;
        }

        /// <summary>
        /// 通过razor模板输出控件
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        public MvcHtmlString RenderItem(Func<PageCommon_SwiperContainer, HelperResult> func)
        {
            var sb = new StringBuilder();
            sb.AppendLine("<div class=\"swiper-slide\">");
            sb.AppendLine(func(this).ToHtmlString());
            sb.AppendLine("</div> <!-- end swiper-slide -->");

            return MvcHtmlString.Create(sb.ToString());
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div> <!-- end swiper-wrapper -->");

            ViewContext.Writer.WriteLine($"<div class=\"{_paginationStyle}\"></div>");

            ViewContext.Writer.WriteLine("</div> <!-- end swiper-container -->");
        }

        internal override ContainerBase RenderInternal()
        {
            ViewContext.ViewBag.HasUseSwiper = true;

            ViewContext.Writer.WriteLine($"<div {_attrs} data-space-between=\'10\' data-autoplay=\"{_playInterval}\">");
            ViewContext.Writer.WriteLine("<div class=\"swiper-wrapper\">");

            return this;
        }

        public class PageCommon_SwiperContainerItem: ContainerBase
        {
            public PageCommon_SwiperContainerItem(ViewContext viewContext) : base(viewContext)
            {
                this.Css("swiper-slide");
            }

            public override void Dispose()
            {
                ViewContext.Writer.WriteLine("</div> <!-- end swiper-slide -->");
            }

            internal override ContainerBase RenderInternal()
            {
                ViewContext.Writer.WriteLine("<div class=\"swiper-slide\">");

                return this;
            }
        }
    }

    public class SwiperItem
    {
        public SwiperItem(string imageUrl, string gotoUrl="#")
        {
            ImageUrl = imageUrl ?? throw new ArgumentNullException(nameof(imageUrl));
            GotoUrl = gotoUrl ?? throw new ArgumentNullException(nameof(gotoUrl));
        }

        public string ImageUrl { set; get; }

        public string GotoUrl { set; get; } = "#";
    }
}